package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.CarStorageActivity;
import com.novellotus.iov.carvice.activity.DiagnosisListActivity;
import com.novellotus.iov.carvice.activity.OperationActivity;
import com.novellotus.iov.carvice.activity.OutGoingActivity;
import com.novellotus.iov.carvice.activity.TeamManagementActivity;
import com.novellotus.iov.carvice.bean.RxEinfos;
import com.novellotus.iov.carvice.utils.MapUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/12/26.
 */

public class WorkFragmentRoleItemAdapter extends RecyclerView.Adapter<WorkFragmentRoleItemAdapter.RoleItemHolder> {

    Context context;
    List<RxEinfos.RolesBean.MenusBean> menusBeanList;
    String eCode;
    String rCode;

    public WorkFragmentRoleItemAdapter(List<RxEinfos.RolesBean.MenusBean> menusBeanList, String eCode, String rCode) {
        this.menusBeanList = menusBeanList;
        this.eCode = eCode;
        this.rCode = rCode;
    }

    @Override
    public RoleItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_work_fragment_item, null);
        return new RoleItemHolder(view);
    }


    @Override
    public void onBindViewHolder(RoleItemHolder holder, final int position) {
        holder.tv.setText(menusBeanList.get(position).getMName());

        holder.img.setBackgroundResource(MapUtils.getMenusIcon(menusBeanList.get(position).getMCode()));
        if (menusBeanList.get(position).getMCode() != null) {
            if (menusBeanList.get(position).getMCode().equals("V_CarInPut")) {
                holder.rlt_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, CarStorageActivity.class);
                        intent.putExtra("eCode", eCode);
                        intent.putExtra("rCode", rCode);
                        context.startActivity(intent);
                    }
                });
            } else if (menusBeanList.get(position).getMCode().equals("A_CarOutPut")) {
                holder.rlt_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, OutGoingActivity.class);
                        intent.putExtra("eCode", eCode);
                        intent.putExtra("rCode", rCode);
                        context.startActivity(intent);
                    }
                });
            } else if (menusBeanList.get(position).getMCode().equals("A_AutoDiagnosis")) {
                holder.rlt_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, DiagnosisListActivity.class);
                        intent.putExtra("eCode", eCode);
                        intent.putExtra("rCode", rCode);
                        context.startActivity(intent);
                    }
                });
            } else if (menusBeanList.get(position).getMCode().equals("C_TEAM")) {
                holder.rlt_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, TeamManagementActivity.class);
                        intent.putExtra("eCode", eCode);
                        intent.putExtra("rCode", rCode);
                        context.startActivity(intent);
                    }
                });

            } else {
                holder.rlt_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, OperationActivity.class);
                        context.startActivity(intent);
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return menusBeanList.size();
    }

    class RoleItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;
        @BindView(R.id.tv)
        TextView tv;
        @BindView(R.id.img)
        ImageView img;

        RoleItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
